package com.yc.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
public class ParamDo {

    @RequestMapping("pv/{p1}/{p2}")
    @ResponseBody
    public String pathVariable(
            @PathVariable String p1,
            @PathVariable("p2") String p3){
        return p1 + " : " + p3;
    }

    @RequestMapping("headers")
    @ResponseBody
    public String headers(
            @RequestHeader String Host,
            @RequestHeader("Accept") String accept
    ){
        return Host + "  :: " + accept;
    }

    @RequestMapping("cookie")
    @ResponseBody
    public String cookie(@CookieValue String name){
        return name;
    }

    @RequestMapping(path = "save",
        produces = "text/html;charset=utf-8")
    @ResponseBody
    public String saveSession(
            String name,
            String value,
            HttpSession session){
        session.setAttribute(name,value);
        return "保存数据到回话中了!";
    }

    @RequestMapping(path = "load",
            produces = "text/html;charset=utf-8")
    @ResponseBody
    public String loadSession(
            // SessionAttribute 从回话中提取属性值
            @SessionAttribute(required = false) String myname){
        return myname==null?"没有myname":myname;
    }

}
